package com.collectplus.express.self;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.model.ReceiverBean;

import java.util.ArrayList;
import java.util.HashSet;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * 备注界面适配器
 * Created by joker on 2018/4/9.
 */
public class SelfRemarkAdapter extends BaseAdapterExt<String> {

    private HashSet<Integer> selected = null;

    private ReceiverBean cacheReceiver = null;

    public SelfRemarkAdapter(ArrayList<String> items, Activity context) {
        super(items, context);
        cacheReceiver = AppCache.getParcelNew();
        if (cacheReceiver == null) {
            cacheReceiver = new ReceiverBean(1);
            AppCache.setParcelNew(cacheReceiver);
        } else {
            cacheReceiver.setType(1);// 自助寄件
        }
        selected = cacheReceiver.getRemarkSelected();
        if (selected == null) {
            selected = new HashSet<>();
            cacheReceiver.setRemarkSelected(selected);
        }
    }

    /**
     * 获取选中的标签
     *
     * @return
     */
    public HashSet<Integer> getSelected() {
        return selected;
    }

    /**
     * 设置选中的标签位置
     */
    public void setSelected(int position) {
        selected.add(position);
        notifyDataSetChanged();
    }

    /**
     * 移除选中的标签进行刷新
     */
    public void removeSelected(int position) {
        if (selected.size() > 0) {
            selected.remove(position);
            notifyDataSetChanged();
        }
    }

    /**
     * 移除全部标签进行刷新
     */
    public void removeSelected() {
        selected.clear();
        notifyDataSetChanged();
    }

    /**
     * 保存选中的集合
     */
    public void saveSelected(){
        cacheReceiver.setRemarkSelected(selected);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.self_remark_grid_item, parent, false);
            holder = new ViewHolder();
            holder.labelName = (TextView) convertView.findViewById(R.id.text_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String item = getItem(position);
        if (selected.size() > 0) {
            if (selected.contains(position)) {
                holder.labelName.setBackgroundResource(R.drawable.self_remark_grid_item_selected_bg);
            } else {
                holder.labelName.setBackgroundResource(R.drawable.self_remark_grid_item_bg);
            }
        } else {
            holder.labelName.setBackgroundResource(R.drawable.self_remark_grid_item_bg);
        }
        holder.labelName.setText(item);
        return convertView;
    }

    private class ViewHolder {
        TextView labelName;
    }
}
